package org.itboys.mobile.service.mongo.site;

import org.apache.commons.lang.StringUtils;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.cook.util.DatetUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.JoinMessage;
import org.itboys.mobile.entity.mongo.JoinValiDateCode;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mongo.SundayMemberRecord;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.service.mongo.web.SundayMemberRecordService;
import org.itboys.mobile.service.mongo.web.SundayMemberService;
import org.itboys.mobile.service.mysql.site.SundaySiteProductService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/9/22
 * 描述：雄安加盟网_留言
 */
@Service
public class JoinSiteMessageService extends BaseMongoService<JoinMessage>{
    @Resource(name = "mobileDs")
    private MongoDataSource mobileDataSource;
    @Autowired
    private SundayMemberService memberService;
    @Autowired
    private SundaySiteProductService productService;
    @Autowired
    private SundayMemberRecordService memberRecordService;
    @Autowired
    private JoinSiteValidateCodeService codeService;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mobileDataSource;
    }

    @Override
    protected Class<JoinMessage> getEntityClass() {
        return JoinMessage.class;
    }

    /**
     * 生成留言  如果该手机号用户没有注册  则新增用户
     * @param name
     * @param sex
     * @param phone
     * @param content
     * @param provinceId
     * @param provinceName
     * @param cityId
     * @param cityName
     * @param districtId
     * @param districtName
     */
    public void save(Long productId,String name,String sex,String phone,String code,String content,Long provinceId,String provinceName,Long cityId,String cityName,
                     Long districtId,String districtName){
        if(StringUtils.isNotBlank(code)){
            codeService.checkValidateCode(phone,code);
        }
        SundayMemberRecord member=null;
            Map<String,Object> param=new HashMap<String,Object>();
            param.put("phone",phone);
            param.put("isDeleted",0);
            List<SundayMemberRecord> members=memberRecordService.list(param);
            if(ListUtil.isNotNull(members)){
                member=members.get(0);
            }else {
                member=new SundayMemberRecord();
                member.setPhone(phone);
                member.setNickName(name);
                member.setUserType(SundayCommonConstants.user_type_member);
                memberRecordService.save(member);
            }
            if(LongUtil.isNotZreo(productId)){
                SundayProduct product=productService.find(productId);
                super.save(new JoinMessage(member.getId(),productId,product.getName(),name,phone,sex,content,1,provinceId,provinceName,cityId,cityName,districtId,districtName));
            }else {
                super.save(new JoinMessage(member.getId(),productId,"",name,phone,sex,content,1,provinceId,provinceName,cityId,cityName,districtId,districtName));
            }


    }

    /**
     * 获取项目更多留言
     * @param request
     * @param productId
     * @return
     */
    public PageResult<JoinMessage> selectMoreMessage(HttpServletRequest request, Long productId){
        Map<String,Object> param= ParamUtil.packageMongoExactParam(request);
        Map<String,String> containParam=ParamUtil.packageMongoVagueParam(request,"name","content");
        param.put("productId",productId);
        param.put("isDeleted",0);
        return super.containpageQuery(request,containParam,param);
    }
    /**
     * 企业获取留言
     * @param request
     * @param productId
     * @return
     */
    public PageResult<JoinMessage> selectProductMessage(HttpServletRequest request, Long productId,Long provinceId,Long cityId,Long districtId,String mobile,
                                                        Integer status,String name,String content){
        Map<String,Object> param= ParamUtil.packageMongoExactParam(request);
        Map<String,String> containParam=ParamUtil.packageMongoVagueParam(request);
        if(LongUtil.isNotZreo(provinceId)){
            param.put("provinceId",provinceId);
        }
        if(LongUtil.isNotZreo(cityId)){
            param.put("cityId",cityId);
        }
        if(LongUtil.isNotZreo(districtId)){
            param.put("districtId",districtId);
        }
        if(status!=null&&status!=0){
            param.put("status",status);
        }
        if(StringUtils.isNotBlank(mobile)){
            containParam.put("mobile",mobile);
        }
       if(StringUtils.isNotBlank(name)){
           containParam.put("name",name);
       }
       if(StringUtils.isNotBlank(content)){
           containParam.put("content",content);
       }
        param.put("productId",productId);
        param.put("isDeleted",0);
         return super.containpageQuery(request,containParam,param);
    }

    /**
     * 用户获取留言
     * @param request
     * @param memberRecordId
     * @return
     */
    public PageResult<JoinMessage> selectMemberMessage(HttpServletRequest request, Long memberRecordId,Integer status){
        Map<String,Object> param= ParamUtil.packageMongoExactParam(request);
        Map<String,String> containParam=ParamUtil.packageMongoVagueParam(request);
        param.put("memberRecordId",memberRecordId);
        if(status!=null&&status!=0){
            param.put("status",status);
        }
        return super.containpageQuery(request,containParam,param);
    }

    /**
     * 批量删除
     * @param ids
     */
    public void batchDelete(List<Long> ids){
        for(Long id:ids){
            super.delete(id);
        }
    }

    /**
     * 获取留言统计
     * @param productId
     * @return
     */
    public Map<String,Object> getIndexMessageNum(Long productId){
        Map<String,Object> result=new HashMap<String,Object>();
        //今日总留言量
        Integer todayNum=0;
        //今日未处理留言数量
        Integer todayNotNum=0;
        //总留言数
        Integer totalNum=0;
        if(LongUtil.isZreo(productId)){
            result.put("todayNum",todayNum);
            result.put("todayNotNum",todayNotNum);
            result.put("totalNum",totalNum);
            return result;
        }
        Map<String,Object> param=new HashMap<String,Object>();


        Long startTime= DatetUtil.setDateStart(new Date()).getTime();
        param.put("productId",productId);
        List<JoinMessage> messages=super.list(param);
        if(ListUtil.isNotNull(messages)){
            totalNum=messages.size();
            for(JoinMessage message:messages){
                if(message.getCt()>startTime.longValue()){
                    todayNum++;
                    if(message.getCt()>startTime.longValue()&&message.getStatus()==1){
                        todayNotNum++;
                    }
                }
            }
        }else {
            return null;
        }
        result.put("todayNum",todayNum);
        result.put("todayNotNum",todayNotNum);
        result.put("totalNum",totalNum);
        return result;
    }

}
